package cli;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;

public class Concert {
	int ref_evt;
	String date;
	String h_debut;
	String h_fin;
	int prix;
	int places;
	String artiste;
	String style;
	
	public Concert(int ref_evt, String date, String h_debut, String h_fin, int prix, int places, String artiste, String style, String file) throws M1Exception, M2Exception, M3Exception, M4Exception, M5Exception {

		this.ref_evt = ref_evt;
		this.date = date;
		this.h_debut = h_debut;
		this.h_fin = h_fin;
		this.prix = prix;
		this.places = places;
		this.artiste = artiste;
		this.style = style;

		
		try {
            SimpleDateFormat format = new SimpleDateFormat("dd/MM/yy");
            format.setLenient(false);
            format.parse(date);
        } catch (ParseException e) {  
        	throw new M2Exception("Erreur M2 : Date incorrecte");
        } catch (IllegalArgumentException e) {
        	throw new M2Exception("Erreur M2 : Date incorrecte");
        }
		
		ArrayList<String> infos = XML.getCreneaux(file);
		Iterator<String>j = infos.iterator();
		
		while(j.hasNext()){
			String information = j.next();
			SimpleDateFormat format = new SimpleDateFormat("dd/MM/yy");
			try {
				Date date_evt = format.parse(information);
				Date jour = format.parse(date);
				if(date_evt.equals(jour)){
					String debut = j.next();
					String fin = j.next();
					SimpleDateFormat format2 = new SimpleDateFormat("HH'h'mm");
					Date end = format2.parse(h_fin);
					Date beginning = format2.parse(h_debut);
					Date heuredebut = format2.parse(debut);
					Date heurefin = format2.parse(fin);
					if(beginning.before(heurefin) || end.after(heuredebut)){
						throw new M3Exception("Erreur M3 : Créneau déjà utilisé");
					}
				}
				else{
					j.next();
					j.next();
				}
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		
		
		
		if(prix<0)
			throw new M4Exception("Erreur M4 : Prix invalide");
		
		if(artiste.equals(""))
			throw new M5Exception("Erreur M5 : Nom d'équipe manquant");
		
		ArrayList<Integer> references = XML.getReferences(file);
		Iterator<Integer> i = references.iterator();
		while(i.hasNext()){
			Integer ref = i.next();
			if(ref == this.ref_evt){;
				throw new M1Exception();
			}
		}
	}
}
